namespace CE.Parsers.C
{
    using System.Windows.Forms;

	using CE.Parsers.CS;

	public class CParser
	{
		#region Methods

		public static void ParseToTree(string fileName, TreeNodeCollection nodes)
		{
			CE.Parsers.C.Scanner scanner = new CE.Parsers.C.Scanner(fileName);
			CE.Parsers.C.Parser parser = new CE.Parsers.C.Parser(scanner);
			parser.Parse();

			// Include...
			TreeNode nInclude = new TreeNode("Includes");
			foreach (TokenMatch tm in parser.CodeInfo.Includes) {
				TreeNode n = new TreeNode(tm.Value);
				n.Tag = tm.Position;
				nInclude.Nodes.Add(n);
			}
			if (nInclude.Nodes.Count > 0) {
				nodes.Add(nInclude);
			}

			// Defines...
			TreeNode nDefine = new TreeNode("Defines");
			foreach (TokenMatch tm in parser.CodeInfo.Defines) {
				TreeNode n = new TreeNode(tm.Value);
				n.Tag = tm.Position;
				nDefine.Nodes.Add(n);
			}
			if (nDefine.Nodes.Count > 0) {
				nodes.Add(nDefine);
			}

			// GlobalVars...
			TreeNode nGlobalVars = new TreeNode("Global Variables");
			foreach (TokenMatch tm in parser.CodeInfo.GlobalVariables) {
				TreeNode n = new TreeNode(tm.Value);
				n.Tag = tm.Position;
				nGlobalVars.Nodes.Add(n);
			}
			if (nGlobalVars.Nodes.Count > 0) {
				nodes.Add(nGlobalVars);
				nGlobalVars.Expand();
			}

			// Structs...
			TreeNode nStructs = new TreeNode("Structs");
			foreach (TokenMatch tm in parser.CodeInfo.Structs) {
				TreeNode n = new TreeNode(tm.Value);
				n.Tag = tm.Position;
				nStructs.Nodes.Add(n);
			}
			if (nStructs.Nodes.Count > 0) {
				nodes.Add(nStructs);
				nStructs.Expand();
			}

			// TypeDefs...
			TreeNode nTypeDefs = new TreeNode("TypeDefs");
			foreach (TokenMatch tm in parser.CodeInfo.TypeDefs) {
				TreeNode n = new TreeNode(tm.Value);
				n.Tag = tm.Position;
				nTypeDefs.Nodes.Add(n);
			}
			if (nTypeDefs.Nodes.Count > 0) {
				nodes.Add(nTypeDefs);
				nTypeDefs.Expand();
			}

			// Prototypes...
			TreeNode nPrototypes = new TreeNode("Prototypes");
			foreach (TokenMatch tm in parser.CodeInfo.Prototypes) {
				TreeNode n = new TreeNode(tm.Value);
				n.Tag = tm.Position;
				nPrototypes.Nodes.Add(n);
			}
			if (nPrototypes.Nodes.Count > 0) {
				nodes.Add(nPrototypes);
				nPrototypes.Expand();
			}

			// Functions...
			TreeNode nFunctions = new TreeNode("Functions");
			foreach (TokenMatch tm in parser.CodeInfo.Functions) {
				TreeNode n = new TreeNode(tm.Value);
				n.Tag = tm.Position;
				nFunctions.Nodes.Add(n);
			}
			if (nFunctions.Nodes.Count > 0) {
				nodes.Add(nFunctions);
				nFunctions.Expand();
			}
		}

		#endregion Methods
	}
}